package cn.sinata.wanglin.neighbor.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.xilada.xldutils.utils.Utils;

import cn.sinata.wanglin.R;

/**
 *
 * Created by liaoxiang on 16/12/19.
 */

public class SearchEditText extends EditText{

    public SearchEditText(Context context) {
        super(context);
        init(context,null);
    }

    public SearchEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    void init(Context context,AttributeSet set){
        int textSize = getResources().getDimensionPixelSize(R.dimen.textSize);
        if (set != null) {
            TypedArray a = context.obtainStyledAttributes(set, R.styleable.SearchEditText);
            Drawable background = a.getDrawable(R.styleable.SearchEditText_android_background);
            if (background==null){
                background = ContextCompat.getDrawable(context,R.drawable.search_bg);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                setBackground(background);
            }else {
                setBackgroundDrawable(background);
            }
            Drawable left = a.getDrawable(R.styleable.SearchEditText_android_drawableLeft);
            Drawable right = a.getDrawable(R.styleable.SearchEditText_android_drawableRight);
            Drawable top = a.getDrawable(R.styleable.SearchEditText_android_drawableTop);
            Drawable bottom = a.getDrawable(R.styleable.SearchEditText_android_drawableBottom);
            if (left == null) {
                left = ContextCompat.getDrawable(context,R.mipmap.ic_search_bg);
            }
            setCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom);
            CharSequence hint = a.getText(R.styleable.SearchEditText_android_hint);
            if (hint == null) {
                hint = "搜索";
            }
            setHint(hint);
            textSize = a.getDimensionPixelSize(R.styleable.SearchEditText_android_textSize,textSize);
            setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);

            ColorStateList textColor = a.getColorStateList(R.styleable.SearchEditText_android_textColor);
            if (textColor == null) {
                textColor = ContextCompat.getColorStateList(context,R.color.textColorHint);
            }
            setTextColor(textColor);
            a.recycle();

        }
        setSingleLine(true);
        setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    Utils.hideSoftWindow(getContext(), textView);
                    if (listener != null) {
                        listener.onSearch();
                    }
                    return true;
                }
                return false;
            }
        });
    }

    private OnSearchActionListener listener;

    public void setOnSearchActionListener(OnSearchActionListener listener) {
        this.listener = listener;
    }

    public interface OnSearchActionListener{
        void onSearch();
    }
}
